//
//  WYCSetDissController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/2.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCSetDissController.h"
#import "WYCOpinionController.h"
#import "WYCSetPassController.h"
#import "WYCHideCardController.h"
#import "WYCLoginController.h"
#import "SetGestureLockViewController.h"
#import "WYCZhifusetController.h"
#import "SetGestureLockViewController.h"
#import "WYCAboutMeController.h"
#import "WYCStrCardController.h"

@interface WYCSetDissController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    BOOL first;
    CGFloat sizeM;
}

@property(nonatomic,strong)UITableView * tableView;

/** 手势按钮*/
@property(nonatomic,strong)UIButton * touchpassbutton;


@property(retain,nonatomic) UISwitch * mySwitch;

/** 手势按钮*/
@property(nonatomic,strong)UIButton * folderbutton;

@end

@implementation WYCSetDissController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //setNav
    [self setNav];
    
    
    //创建表视图
    [self setupTableView];
    
}

#pragma make -------->>TableView
- (void)setupTableView{
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight, ScreenWidth, WYCcellHeight * 7 + 30 * px) style:UITableViewStylePlain];
    //    self.tableView.backgroundColor = [UIColor redColor];
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    
    
    UIButton * cellbutton = [[UIButton alloc] init];
    cellbutton.frame = CGRectMake(0, CGRectGetMaxY(self.tableView.frame)+ 60 * px, ScreenWidth, WYCcellHeight);
    cellbutton.titleLabel.font = [UIFont systemFontOfSize:WyzFont];
    [cellbutton setTitleColor:WYCMain forState:UIControlStateNormal];
    cellbutton.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
    [cellbutton addTarget:self action:@selector(cellButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [cellbutton setTitle:@"退出登录" forState:UIControlStateNormal];
    cellbutton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:cellbutton];
    
}
#pragma make ------->>setNav
- (void)setNav {
//    self.navigationItem.title = @"设置";
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//    [self.navigationController.navigationBar setBarTintColor:WYCMain];
    
    self.view.backgroundColor = WYCGlobalBg;
}

#pragma make ------->>退出登录点击事件
- (void)cellButtonClick{
    WYCLogFunc;
    NSString * str = [NSString stringWithFormat:@"%@token=%@",Userlogout,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:str params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"%@",responseObject);
        if ([[responseObject objectForKey:@"code"]intValue] == 0) {
            [self showToast:@"退出成功"];
            WYCLoginController * login = [[WYCLoginController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:login];
            [self presentViewController:nav animated:NO completion:nil];
            [WYCAccountTool clearAccount];
            [WYCIsAppLoginTool clearIsAppLogin];
            NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
            
            // 保存用户数据
            [useDef removeObjectForKey:@"first"];
        }else{
            [self showToast:msgStr];
            NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
            
            // 保存用户数据
            [useDef removeObjectForKey:@"first"];
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
            [self showToast:Failed];
        NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
        
        // 保存用户数据
            [useDef removeObjectForKey:@"first"];
    }];
    
    
    
}

#pragma make ------->>UItableView代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 8;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 4) {
        return 30 * px;
    }else{
        return 160 * px;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    tableView.tableFooterView = [[UIView alloc] init];
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"123"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"123"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    if (indexPath.row == 0 || indexPath.row == 4) {
        
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }
    
    CGFloat celltitleX = 40 * px;
    CGFloat celltitleW = 240 * px;
    
    CGFloat celltitleH = 60 * px;
    
    if (indexPath.row == 0) {
        UIView * cellView = [[UIView alloc] init];
        cellView.frame = CGRectMake(0, 0, ScreenWidth, cell.height);
        [cell addSubview:cellView];
        
        
        UILabel * pushtitle = [[UILabel alloc] init];
        pushtitle.frame = CGRectMake(celltitleX, celltitleX, celltitleW, celltitleH);
        pushtitle.text = @"手势密码";
        pushtitle.font = [UIFont systemFontOfSize:WyzFont];
        [cellView addSubview:pushtitle];
        
        

        _mySwitch = [[UISwitch alloc]init];
        _mySwitch.frame=CGRectMake(ScreenWidth - 200 * px, 20 * px, 80 * px, 20);
        
        
        NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
    
        
        
        if ([useDef objectForKey:@"first"]) {
            
            [_mySwitch setOn:YES animated:NO];
            _mySwitch.onTintColor = [UIColor greenColor];
            _mySwitch.thumbTintColor = [UIColor whiteColor];
        } else {
            [_mySwitch setOn:NO animated:true];
            _mySwitch.tintColor = [UIColor grayColor];
            _mySwitch.thumbTintColor = [UIColor grayColor];
            [_mySwitch setBackgroundColor:[UIColor whiteColor]];
        }
        
        [cell addSubview:_mySwitch];
        
        
        

        [_mySwitch addTarget:self action:@selector(swChange:) forControlEvents:UIControlEventValueChanged];
        
        
        UIView * line = [[UIView alloc] init];
        line.frame = CGRectMake(40 * px, cellView.height - 0.5, cellView.width - 80 * px, 0.5);
        line.backgroundColor = WYCcellLINEColor;
        [cellView addSubview:line];
    }else if (indexPath.row == 1){
        UIView * cellView = [[UIView alloc] init];
        cellView.frame = CGRectMake(0, 0, ScreenWidth, cell.height);
        [cell addSubview:cellView];
        
        
        UILabel * pushtitle = [[UILabel alloc] init];
        pushtitle.frame = CGRectMake(celltitleX, celltitleX, celltitleW, celltitleH);
        pushtitle.text = @"修改密码";
        pushtitle.font = [UIFont systemFontOfSize:WyzFont];
        [cellView addSubview:pushtitle];
        
        UIView * line = [[UIView alloc] init];
        line.frame = CGRectMake(40 * px, cellView.height - 0.5, cellView.width - 80 * px, 0.5);
        line.backgroundColor = WYCcellLINEColor;
        [cellView addSubview:line];
        
    }else if (indexPath.row == 2){
        UIView * cellView = [[UIView alloc] init];
        cellView.frame = CGRectMake(0, 0, ScreenWidth, cell.height);
        [cell addSubview:cellView];
        
        
        UILabel * pushtitle = [[UILabel alloc] init];
        pushtitle.frame = CGRectMake(celltitleX, celltitleX, celltitleW, celltitleH);
        pushtitle.text = @"支付设置";
        pushtitle.font = [UIFont systemFontOfSize:WyzFont];
        [cellView addSubview:pushtitle];
        
        
        UIView * line = [[UIView alloc] init];
        line.frame = CGRectMake(40 * px, cellView.height - 0.5, cellView.width - 80 * px, 0.5);
        line.backgroundColor = WYCcellLINEColor;
        [cellView addSubview:line];
        
    }else if (indexPath.row == 3){
        UIView * cellView = [[UIView alloc] init];
        cellView.frame = CGRectMake(0, 0, ScreenWidth, cell.height);
        [cell addSubview:cellView];
        
        
        UILabel * pushtitle = [[UILabel alloc] init];
        pushtitle.frame = CGRectMake(celltitleX, celltitleX, celltitleW * 2, celltitleH);
        pushtitle.text = @"隐藏卡片管理";
        pushtitle.font = [UIFont systemFontOfSize:WyzFont];
        [cellView addSubview:pushtitle];
        
    }else if (indexPath.row == 4){
        UIView * cellView = [[UIView alloc] init];
        cellView.frame = CGRectMake(0, 0, ScreenWidth,  30 * px);
        [cell addSubview:cellView];
        cellView.backgroundColor = WYCGlobalBg;
        
    }else if (indexPath.row == 5){
        UIView * cellView = [[UIView alloc] init];
        cellView.frame = CGRectMake(0, 0, ScreenWidth, cell.height);
        [cell addSubview:cellView];
        
        
        UILabel * pushtitle = [[UILabel alloc] init];
        pushtitle.frame = CGRectMake(celltitleX, celltitleX, celltitleW, celltitleH);
        pushtitle.text = @"意见反馈";
        pushtitle.font = [UIFont systemFontOfSize:WyzFont];
        [cellView addSubview:pushtitle];
        
        UIView * line = [[UIView alloc] init];
        line.frame = CGRectMake(40 * px, cellView.height - 0.5, cellView.width - 80 * px, 0.5);
        line.backgroundColor = WYCcellLINEColor;
        [cellView addSubview:line];
        
    }else if (indexPath.row == 6){
        UIView * cellView = [[UIView alloc] init];
        cellView.frame = CGRectMake(0, 0, ScreenWidth, cell.height);
        [cell addSubview:cellView];
        
        
        UILabel * pushtitle = [[UILabel alloc] init];
        pushtitle.frame = CGRectMake(celltitleX, celltitleX, celltitleW, celltitleH);
        pushtitle.text = @"清楚缓存";
        pushtitle.font = [UIFont systemFontOfSize:WyzFont];
        [cellView addSubview:pushtitle];
        
        self.folderbutton = [[UIButton alloc] init];
        self.folderbutton.titleLabel.font = [UIFont systemFontOfSize:SmallFont];
        [self.folderbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.folderbutton.frame = CGRectMake(ScreenWidth - 300 * px, celltitleX, 220 * px, celltitleH);
        //        self.folderbutton.backgroundColor = [UIColor redColor];
        NSString *str = [NSString stringWithFormat:@"%.2fM",sizeM];
        
        [self.folderbutton addTarget:self action:@selector(folderButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.folderbutton setTitle:str forState:UIControlStateNormal];
        WYCLog(@"=======%@",self.folderbutton.titleLabel.text);
        [cellView addSubview:self.folderbutton];
        
        UIView * line = [[UIView alloc] init];
        line.frame = CGRectMake(40 * px, cellView.height - 0.5, cellView.width - 80 * px, 0.5);
        line.backgroundColor = WYCcellLINEColor;
        [cellView addSubview:line];
        
    }else {
        UIView * cellView = [[UIView alloc] init];
        cellView.frame = CGRectMake(0, 0, ScreenWidth, cell.height);
        [cell addSubview:cellView];
        
        
        UILabel * pushtitle = [[UILabel alloc] init];
        pushtitle.frame = CGRectMake(celltitleX, celltitleX, celltitleW, celltitleH);
        pushtitle.text = @"关于我们";
        pushtitle.font = [UIFont systemFontOfSize:WyzFont];
        [cellView addSubview:pushtitle];
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 1) {
        WYCSetPassController * SetPass = [[WYCSetPassController alloc] init];
        [self.navigationController pushViewController:SetPass animated:NO];
    }
    if (indexPath.row == 2) {
        WYCZhifusetController * mewallet = [[WYCZhifusetController alloc] init];
        [self.navigationController pushViewController:mewallet animated:NO];
    }
    
    if (indexPath.row == 3) {
        WYCStrCardController * hideCard = [[WYCStrCardController alloc] init];
        [self.navigationController pushViewController:hideCard animated:NO];
    }
    
    if (indexPath.row == 5) {
        WYCOpinionController * opininon = [[WYCOpinionController alloc] init];
        [self.navigationController pushViewController:opininon animated:NO];
    }
    if (indexPath.row == 7) {
        WYCAboutMeController * opininon = [[WYCAboutMeController alloc] init];
        [self.navigationController pushViewController:opininon animated:NO];
    }
    
}
//参数传入开关对象本身
- (void) swChange:(UISwitch*) sw{
    
    if(sw.on == YES){
        first = YES;
        
        [_mySwitch setOn:YES animated:true];
//        _mySwitch.tintColor = [UIColor whiteColor];
        _mySwitch.thumbTintColor = [UIColor whiteColor];
        _mySwitch.onTintColor = [UIColor greenColor];
        NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
        
        // 保存用户数据
        
        [useDef setBool:first forKey:@"first"];
        
        [useDef synchronize];
        SetGestureLockViewController * mewallet = [[SetGestureLockViewController alloc] init];
        mewallet.string = YES;
        [self.navigationController pushViewController:mewallet animated:NO];
    }else{
//        first = NO;
        [_mySwitch setOn:NO animated:true];
        _mySwitch.tintColor = [UIColor grayColor];
        _mySwitch.thumbTintColor = [UIColor grayColor];
        
        NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
        
        // 保存用户数据
        [useDef removeObjectForKey:@"first"];
        //        [useDef setBool:first forKey:@"first"];
        
        [useDef synchronize];
    }
}

// 缓存大小
- (CGFloat)folderSize{
    CGFloat folderSize = 0.0;
    
    //获取路径
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)firstObject];
    
    //获取所有文件的数组
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];
    
    NSLog(@"文件数：%ld",files.count);
    
    for(NSString *path in files) {
        
        NSString*filePath = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",path]];
        
        //累加
        folderSize += [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil].fileSize;
    }
    //转换为M为单位
    sizeM = folderSize /1024.0/1024.0;
    
    
    
    return sizeM;
}
- (void)folderButtonClick{
    
    //===============清除缓存==============
    //获取路径
    NSString*cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)objectAtIndex:0];
    
    //返回路径中的文件数组
    NSArray*files = [[NSFileManager defaultManager]subpathsAtPath:cachePath];
    
    NSLog(@"文件数：%ld",[files count]);
    for(NSString *p in files){
        NSError*error;
        
        NSString*path = [cachePath stringByAppendingString:[NSString stringWithFormat:@"/%@",p]];
        
        if([[NSFileManager defaultManager]fileExistsAtPath:path])
        {
            BOOL isRemove = [[NSFileManager defaultManager]removeItemAtPath:path error:&error];
            if(isRemove) {
                NSLog(@"清除成功");
                [self showToast:@"缓存已清空"];
                [self.folderbutton setTitle:@"0.00M" forState:UIControlStateNormal];
                
            }else{
                
                NSLog(@"清除失败");
                
            }
        }
    }
    
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    
    CALayer *bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCmainColor.CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    
    
    
    
    
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"设置";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
